import json
from xml.dom.minidom import parseString

from google.appengine.api import urlfetch

from models import Libraries

import webapp2


LIBRARIES_URL = 'https://code.google.com/p/cs/codesearch/codesearch/json?dir_info_request=b&file_spec=b&package_name=chromium&name=src%2Fthird_party&file_spec=e&retrieve_deleted=false&fetch_only_modified_files=false&dir_info_request=e&dir_info_request=b&file_spec=b&package_name=chromium&name=src%2Fthird_party&file_spec=e&retrieve_deleted=false&fetch_only_modified_files=false&dir_info_request=e'

LIBRARY_BASE_URL = 'http://src.chromium.org/svn/trunk/src/third_party/'


class UpdateChromiumLibrariesTaskPage(webapp2.RequestHandler):

    def get(self):

        result = urlfetch.fetch(LIBRARIES_URL)
        if result.status_code == 200:
            data = json.loads(result.content)
            library_urls = []
            for result in data['dir_info_response'][0]['child']:
                if result['is_directory']:
                    library_urls.append(LIBRARY_BASE_URL+result['name'])

            self.response.write(library_urls);
            libraries = []
            for library_url in library_urls:
                readme_url = library_url + '/README.chromium'
                readme_result = urlfetch.fetch(readme_url)
                print readme_url
                if readme_result.status_code == 200:
                    text = readme_result.content
                    key = None
                    library = {}
                    for line in text.split('\n'):
                        if not line:
                            if key in library:
                                key = None
                            continue
                        if line.endswith(':'):
                            key = line[:-1]
                        elif key:
                            print 'elif key', key
                            if key in library:
                                library[key] += line
                            else:
                                library[key] = line
                        elif ': ' in line and line[0].isupper():
                            key, value = line.split(': ', 1)
                            library[key] = value.strip()
                            key = None

                    libraries.append(library)

            Libraries.update(libraries)

app = webapp2.WSGIApplication([
    ('/tasks/update-chromium-libraries', UpdateChromiumLibrariesTaskPage),
], debug=True)
